package com.jf.identity.controller;

import com.jf.identity.entity.GatherInfo;
import com.jf.identity.entity.GatherInfoFeedBack;
import com.jf.identity.entity.GatherProcess;
import com.jf.identity.entity.ResponseBean;
import com.jf.identity.exception.ServiceException;
import com.jf.identity.service.IdentityGatherService;
import com.jf.identity.util.Params;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by x on 2018/4/14.
 */
@Controller
@RequestMapping("/vis/")
public class IdentityGatherController {
    @Resource
    private IdentityGatherService identityGatherService;

    @RequestMapping(value = "set-identity-gather-process-info")
    @ResponseBody
    public ResponseBean setIdentityGatherProcessInfo(@Validated @RequestBody GatherProcess gatherProcess,
                                                     BindingResult bindingResult) {
        Params.beanValidate(bindingResult);
//        if (StringUtils.isEmpty(gatherProcess.getBMDBH()) ||
//                StringUtils.isEmpty(gatherProcess.getBMDMC()) ||
//                StringUtils.isEmpty(gatherProcess.getCount()) ||
//                StringUtils.isEmpty(gatherProcess.getTotal())
//                ) {
//            throw new ServiceException("请输入完整采集阶段进度信息");
//        }
        //todo:进入服务层，将数据保存至manggodb
        boolean b = identityGatherService.insertIdentityGatherProcessInfo(gatherProcess);
        return new ResponseBean(b);
    }

    @RequestMapping(value = "set-identity-gatherinfo")
    @ResponseBody
    public ResponseBean setIdentityGatherInfo(@Validated @RequestBody GatherInfoFeedBack gatherInfoFeedBack,
                                              BindingResult bindingResult) {
        Params.beanValidate(bindingResult);
        //todo:对传入的设备信息做空值判断，上限判断（上限为100条）,采集类型判断
        if (StringUtils.isEmpty(gatherInfoFeedBack.getGatherInfo())) {
            throw new ServiceException("身份信息不能为空");
        }
        if (gatherInfoFeedBack.getGatherInfo().size() > 100) {
            throw new ServiceException("身份信息不能超过100条");
        }
//        List<GatherInfo> gatherInfoList = gatherInfoFeedBack.getGatherInfo();
//        for (GatherInfo i : gatherInfoList) {
////            if (StringUtils.isEmpty(i.getSFZJLXM()) ||
////                    StringUtils.isEmpty(i.getSFZJH()) ||
////                    StringUtils.isEmpty(i.getCJLX()) ||
////                    StringUtils.isEmpty(i.getCJSJ()) ||
////                    StringUtils.isEmpty(i.getCJSB()) ||
////                    StringUtils.isEmpty(i.getSBCSDM()) ||
////                    StringUtils.isEmpty(i.getCJDZ())
////                    ) {
////                throw new ServiceException("请输入完整采集信息");
////            }
//            if (!"0".equals(i.getCJLX()) &&
//                    !"1".equals(i.getCJLX()) &&
//                    !"2".equals(i.getCJLX())) {
//                throw new ServiceException("采集类型   0：证件图片 1:面部照片 2：指纹");
//            }
//        }

        //todo:进入服务层，将数据保存至manggodb
        boolean b = identityGatherService.insertIdentityGatherInfo(gatherInfoFeedBack.getGatherInfo());
        return new ResponseBean(b);
    }
}
